package com.mall.service.commodity.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.common.CommonPage;
import com.mall.model.domain.PmsProductCategory;
import com.mall.model.domain.PmsProductCategoryAttributeRelation;
import com.mall.model.dto.ProductCategoryParam;
import com.mall.model.vo.CategoryChildrenVo;
import com.mall.service.commodity.mapper.CategoryAndAttuteMapper;
import com.mall.service.commodity.mapper.PmsProductCategoryMapper;

import org.checkerframework.checker.units.qual.C;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName PmsProductCategoryService
 * @Description 商品分类逻辑层
 * @Author dcq
 * @Date 2023/3/12 19:40
 * @Version 1.0
 */
@Service
public class PmsProductCategoryService {
    @Autowired
    private PmsProductCategoryMapper pmsProductCategoryMapper;
    @Autowired
    private CategoryAndAttuteMapper categoryAndAttuteMapper;
    //添加
    public void createSei(PmsProductCategory pmsProductCategory) {
         pmsProductCategoryMapper.insert(pmsProductCategory);
    }
   //删除

    public void seleteSei(Integer id) {
        pmsProductCategoryMapper.deleteById(id);
    }
   //查询所有一级分类及子分类
    public List<CategoryChildrenVo>  listWcsei() {
        // 找pid = 0
        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductCategory::getParentId, 0);
        List<PmsProductCategory> pmsProductCategories = pmsProductCategoryMapper.selectList(queryWrapper);
        // 查子菜单
        List<CategoryChildrenVo> list = pmsProductCategories.stream().map((productCategory)->{
            CategoryChildrenVo vo = new CategoryChildrenVo();
            BeanUtils.copyProperties(productCategory, vo);
            vo.setChildren(new ArrayList<>());
            vo.getChildren().add(getWcsei(vo));
            return vo;
        }).collect(Collectors.toList());


        return list;
    }

    private CategoryChildrenVo getWcsei(CategoryChildrenVo vo) {
        //1.初始化子表
        vo.setChildren(new ArrayList<>());

        //2.找孙表
        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductCategory::getParentId, vo.getId());
        List<PmsProductCategory> categories = pmsProductCategoryMapper.selectList(queryWrapper);

        //3.对孙表遍历
        categories.stream().forEach((productCategory)->{
            CategoryChildrenVo newVo = new CategoryChildrenVo();
            BeanUtils.copyProperties(productCategory, newVo);
            if (vo.getChildren() == null) {
                vo.setChildren(new ArrayList<>());
            }
            vo.getChildren().add(getWcsei(newVo));
        });

        return  vo;
    }
  //分页查询商品分类
    public CommonPage page(Integer pageNum, Integer pageSize, Integer parentId) {
        Page<PmsProductCategory> myPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsProductCategory>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(parentId!=null,PmsProductCategory::getParentId,parentId);
        Page<PmsProductCategory> pmsProductCategoryPage = pmsProductCategoryMapper.selectPage(myPage, queryWrapper);
        CommonPage page = CommonPage.getPage(pmsProductCategoryPage);
        return page;
    }
    //修改导航栏显示状态
    public void updateSei(String[] ids, Integer navStatus) {
        for (String id : ids) {
            PmsProductCategory pmsProductCategory = pmsProductCategoryMapper.selectById(id);
            pmsProductCategory.setNavStatus(navStatus);
            pmsProductCategoryMapper.updateById(pmsProductCategory);
        }
    }
    //修改显示状态
    public void updateSsei(String[] ids, Integer showStatus) {
        for (String id : ids) {
            PmsProductCategory pmsProductCategory = pmsProductCategoryMapper.selectById(id);
            pmsProductCategory.setShowStatus(showStatus);
            pmsProductCategoryMapper.updateById(pmsProductCategory);
        }
    }
   //修改商品分类
    public void updateIdSei(Integer id, ProductCategoryParam productCategory) {
        PmsProductCategory pmsProductCategory = new PmsProductCategory();
        BeanUtils.copyProperties(productCategory,pmsProductCategory);
        pmsProductCategory.setId(Convert.toLong(id));
        pmsProductCategoryMapper.updateById(pmsProductCategory);
        Integer[] productAttributeIdList = productCategory.getProductAttributeIdList();
        //构造条件  删除 修改之前对应的分类
        LambdaQueryWrapper<PmsProductCategoryAttributeRelation>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,PmsProductCategoryAttributeRelation::getProductCategoryId,id);
        categoryAndAttuteMapper.delete(queryWrapper);
        for (Integer integer : productAttributeIdList) {
            PmsProductCategoryAttributeRelation body = new PmsProductCategoryAttributeRelation();
            body.setProductCategoryId(Convert.toLong(id));
            body.setProductAttributeId(Convert.toLong(integer));
            categoryAndAttuteMapper.insert(body);
        }


    }
    //根据id获取商品分类
    public PmsProductCategory getById(Integer id) {
        PmsProductCategory pmsProductCategory = pmsProductCategoryMapper.selectById(id);
        return  pmsProductCategory;
    }
}